using System.Collections.Generic;

namespace TXK
{
    /// <summary>
    /// 字节集合类
    /// </summary>
    public class ByteArray
    {
        private List<byte> list = new List<byte>();

        public byte this[int index]
        {
            get
            {
                return list[index];
            }
            set
            {
                list[index] = value;
            }
        }

        public int Length => list.Count;

        /// <summary>
        /// 属性，返回字节数组
        /// </summary>
        public byte[] array => list.ToArray();

        /// <summary>
        /// 清空字节数组
        /// </summary>
        public void Clear()
        {
            list = new List<byte>();
        }

        /// <summary>
        /// 添加一个字节
        /// </summary>
        /// <param name="item">字节</param>
        public void Add(byte item)
        {
            list.Add(item);
        }

        /// <summary>
        /// 添加一个字节数组
        /// </summary>
        /// <param name="items">字节数组</param>
        public void Add(byte[] items)
        {
            list.AddRange(items);
        }

        /// <summary>
        /// 添加一个ByteArray对象
        /// </summary>
        /// <param name="byteArray">ByteArray对象</param>
        public void Add(ByteArray byteArray)
        {
            list.AddRange(byteArray.array);
        }

        public void AddWord(ushort value)
        {
            list.Add((byte)(value >> 8));
            list.Add((byte)value);
        }
    }
}
